import { Component, OnInit, ElementRef, Renderer2 } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Router, NavigationExtras } from '@angular/router';
import { NzModalService } from 'ng-zorro-antd/modal';
import { BankTransferList, BankTransferService } from 'src/app/service/business/bank-transfer.service';
import { MessageService } from 'src/app/service/message.service';
import { UserBindService } from 'src/app/service/user-bind.service';

@Component({
  selector: 'app-transfer-list',
  templateUrl: './transfer-list.component.html',
  styleUrls: ['./transfer-list.component.scss']
})
export class TransferListComponent implements OnInit {

  colNames = ['用户编号', '办理类型', '账户名称', '地址'];
  colNames2 = ['用户编号', '办理类型', '账户名称', '开户银行', '银行账号'];
  operates = [
    // { key: '详情', router: '', param: {} },
    // { key: '修改', router: '', param: {} },
    // { key: '删除', router: '', param: {} }
  ];
  operates2 = [
    { key: '详情', router: '', param: {} },
  ];
  dataSet: BankTransferList[] = [];
  dataSet2: any[] = [];
  resultList = [];
  resultList2 = [];

  constructor(
    public router: Router,
    private bankTransferService: BankTransferService,
    private modalService: NzModalService,
    private message: MessageService,
    private userBind: UserBindService
  ) { }

  ngOnInit() {
    this._getBankTransferList();
  }

  tabsetSelectChange(event: any){
    console.log(event.index);
    if(event.index === 0){
      this._getBankTransferList();
    }
    if(event.index === 1){
      this._getBankTransferChangeList();
    }
  }

  _getBankTransferList(pageNumber = 1, pageSize = 5){
    if(!this.nzTableLoading){
      this.nzTableLoading = true;
    }

    this.bankTransferService.getBankTransferList(pageNumber, pageSize).subscribe(res => {
      if(res){
        this.nzTotal = res.totalNumber;
        this.resultList = res.resultList;

        let dataSet: BankTransferList[] = [];
        res.resultList.forEach((item: any) => {
          let data: BankTransferList = {} as BankTransferList;
          data.id = item.id;
          data.bankId = item.bankId;
          data.usageCode = item.usageCode;
          data.businessType = item.businessType === '1' ? '取消银行划扣' : '办理银行划扣';
          data.balanceName = this.getPropertyValue(item.balanceName);
          data.address = '-';
          // data.bankName = this.getPropertyValue(item.bankName);
          // data.account = this.getPropertyValue(item.account);
          data.pdfId = item.waterAgreementNo;

          dataSet.push(data);
        })
        // this.dataSet = dataSet;
        this.getUsageCodeList(dataSet)
      }
      this.nzTableLoading = false;
    }, error => {
      this.nzTableLoading = false;
    })
  }
  nzTableLoading2 = false;
  _getBankTransferChangeList(pageNumber = 1, pageSize = 5){
    this.nzTableLoading2 = true;
    this.bankTransferService.getBankTransferChangeList(pageNumber, pageSize).subscribe(res => {
      this.nzTableLoading2 = false;
      if(res){
        this.nzTotal2 = res.totalNumber;
        this.resultList2 = res.resultList;

        let dataSet: any = [];
        res.resultList.forEach((item: any) => {
          let data = {} as any;
          data.id = item.id;
          data.bankId = item.bankId;
          data.usageCode = item.usageCode;
          data.businessType = item.businessType === '1' ? '取消银行划扣' : '办理银行划扣';
          data.balanceName = this.getPropertyValue(item.accountName);
          data.bankName = this.getPropertyValue(item.bankName);
          data.account = this.getPropertyValue(item.account);
          data.pdfId = item.waterAgreementNo;

          dataSet.push(data);
        })
        this.dataSet2 = dataSet as any;

      }
    }, error => {
      this.nzTableLoading2 = false;
    })

  }
  getPropertyValue(value: string){
    return value || '-'
  }

  // 新增
  addBankTransfer(){
    this.bankTransferService.pageType = 2;
    this.router.navigate(['/me/bank-trans/confirm']);
  }

  handleBack(e: any) {
    let key = e.key;
    let data = e.data;

    // if(key === 0 || key === 1){
    //   this._setPageData(key, data.usageCode, data);
    // }

    if(key === 0){
      this._showDeleteConfirm(data.usageCode);
    }
  }
  _setPageData(type: any, usageCode: string, data: any){
    this.bankTransferService.pageType = type;
    this.bankTransferService.pageData = this.resultList.filter((item: any) => {
      return item.usageCode === usageCode;
    })[0];
    // this.bankTransferService.pageData=data;
    this.router.navigate(['/me/bank/confirm']);
  }

  // 3. 删除
  _deleteBankTransfer(usageCode: string){
    this.bankTransferService.deleteBankTransfer(usageCode).subscribe(res => {
      this.message.success('删除成功');
      this._getBankTransferList();
    }, error => {
      // this.util.createErrorMessage(error);
    })
  }
  _showDeleteConfirm(usageCode: string){
    // this.util.confirm({
    //   nzTitle: '确定要删除此条 银行划扣信息 吗?',
    //   nzContent: '<b style="color: red;">点击确定按钮删除，不可恢复!</b>',
    //   nzOkText: '确定',
    //   nzOkType: 'danger',
    //   nzOnOk: () => {
    //     this._deleteBankTransfer(usageCode);
    //   },
    //   nzCancelText: '取消',
    //   nzOnCancel: () => console.log('Cancel')
    // });
    // this.util.confirm('确定要删除此条 银行划扣信息 吗?点击确定按钮删除，不可恢复!', () => {
    //   this._deleteBankTransfer(usageCode);
    // })
  }


  handleBack2(data: any){
    this.bankTransferService.pageType = 0;
    this.bankTransferService.pageData = this.resultList2.find((item: any) => {
      return item.id === data.id;
    });
    this.router.navigate(['/me/bank-trans/confirm']);
  }
  nzTotal2 = 0;

  nzPageSizeChange2(e: any){
    this._getBankTransferChangeList(1, e.pageSize);
  }
  nzPageIndexChange2(e: any){
    this._getBankTransferChangeList(e.pageNumber, e.pageSize);
  }



  // 分页
  nzTableLoading = true;
  nzTotal = 0;

  nzPageSizeChange(e: any){
    this._getBankTransferList(1, e.pageSize);
  }
  nzPageIndexChange(e: any){
    this._getBankTransferList(e.pageNumber, e.pageSize);
  }

  usageCodeList = []
  getUsageCodeList(dataSet: any){
    this.userBind._getUsageCodeInfo().subscribe(res => {
      this.usageCodeList = res
      dataSet.forEach((item: any) => {
        const codeItem: any = this.usageCodeList.find((codeItem: any) => codeItem.usageCode === item.usageCode)
        if(codeItem){
          item.address = codeItem.address
        }

        return item
      })
      console.log(dataSet)
      this.dataSet = []
      this.dataSet = dataSet
    })
  }
}
